from rest_framework.authentication import BaseAuthentication
from utils.myjwt import pymyjwt
from rest_framework.exceptions import AuthenticationFailed
import jwt
from stu.models import usermodel
class MyAuthentication(BaseAuthentication):
  def authenticate(self, request):
      token=request.META.get("HTTP_AUTHORIZATION",None)
      # Barer token
      if token:
          tokendata=token.split(" ")[1]
          try:
              result=pymyjwt.checktoken(tokendata)
              #去表中查询数据
              user=usermodel.objects.get(id=result["id"])
              return (user,tokendata)
          except jwt.ExpiredSignatureError:
              raise AuthenticationFailed("token过期")
          except jwt.InvalidTokenError:
              raise AuthenticationFailed("token无效")